#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
#include"BlockQueue.hpp"
#include"Task.hpp"
using namespace std;

void* productor(void* arg)
{
    BlockQueue<Task>* q=static_cast<BlockQueue<Task>*>(arg);

    while(true)
    {
        int data1=rand()%10+1; //[1,10]
        
        //data1和data2因为执行太快，产生的值可能一样
        usleep(rand()%123);

        int data2=rand()%10+1; //[1,10]
        usleep(rand()%123);
        char oper = opers[rand()%opers.size()];

        Task t(data1,data2,oper);

        q->Push(t);

        string task_string = t.PrintTask();
        cout<<"productor task: "<<task_string<<endl;

        //sleep(1);
    }
    return nullptr;
}

void* consumer(void* arg)
{

    BlockQueue<Task>* q=static_cast<BlockQueue<Task>*>(arg);
    while(true)
    {
        sleep(1);

        Task t;
        q->Pop(&t);
        t();

        string task_string = t.PrintResult();
        cout<<"consumer result: "<<task_string<<endl;
    }
    return nullptr;
}

int main()
{
    srand((uint16_t)time(nullptr)^getpid()^pthread_self());
    BlockQueue<Task>* q = new BlockQueue<Task>();

    pthread_t pro,con;

    pthread_create(&pro,nullptr,consumer,(void*)q);
    pthread_create(&con,nullptr,productor,(void*)q);

    pthread_join(pro,nullptr);
    pthread_join(con,nullptr);
}